all files / src/validators/ stock.validator.ts

94.74% Statements 18/19
87.5% Branches 7/8
100% Functions 3/3
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27     12×   11× 11× 11×   11×           13× 20×    
import { Game, Phase } from '../game.interfaces';
import { getUniqueBidders, getBidWinner } from '../helpers/bid.helpers';
import * as _ from 'lodash';
import { ShareStock } from '../game.actions';
import { hasEightCards, getCard } from '../helpers/cards.helpers';
 
export function canShareStock(state: Game, action: ShareStock): boolean {
    if(state.phase !== Phase.SHARE_STOCK) { return false; }
 
    const winnerPlayerId = getBidWinner(state.bid).player;
    const targetPlayerId = action.targetPlayer;
    const playerId = action.player;
 
    if(playerId !== winnerPlayerId) { return false; }
 
    const targetPlayerCards = state.cards[targetPlayerId];
 
    Iif(!targetPlayerCards || targetPlayerCards.length === 8) { return false; }
 
    return !!getCard(state.cards[playerId], action.card);
}
 
export function isSharingStockFinished(state: Game): boolean {
    return _.chain(getUniqueBidders(state.bid))
        .every(playerId => hasEightCards(state.cards[playerId]))
        .value();
}